/*
Copyright 2025 PerfXLab (Beijing) Technologies Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <vector>
#include <random>
#include <algorithm>
#include <cstdlib>

template <typename T>
inline T random_value()
{
    if constexpr (std::is_same<T, int32_t>::value)
    {
        return std::rand();
    }
    else
    {
        return static_cast<T>(std::rand()) / RAND_MAX;
    }
}

// Generate a randomly sorted array of values 0-(n-1).
inline std::vector<OpenSpB_index> generate_a_random_index(
    OpenSpB_index n)
{
    std::vector<OpenSpB_index> a;
    std::random_device rd;
    std::mt19937 g(rd());

    for (OpenSpB_index i = 0; i < n; ++i)
    {
        a.push_back((OpenSpB_index)i);
    }
    std::shuffle(a.begin(), a.end(), g);
    return a;
}

inline std::vector<OpenSpB_index> generate_index(
    OpenSpB_index n)
{
    std::vector<OpenSpB_index> a;

    for (OpenSpB_index i = 0; i < n; ++i)
    {
        a.push_back((OpenSpB_index)i);
    }

    return a;
}

template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
generate_random_number()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<T> dist(0.0, 1.0);
    return dist(gen);
}

// 生成整数类型随机数
template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
generate_random_number()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<T> dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
    return dist(gen);
}
